def Server(IP):
    HOST = IP
    PORT = 55555
    BUFSIZ = 4096
    ADDR = (HOST, PORT)
    tcpServSock = socket(AF_INET, SOCK_STREAM)
    tcpServSock.bind(ADDR)
    tcpServSock.listen(5)
    SVNPATH='D:\\svn\\SVN.EXE'
    print('服务器监听程序已经启动 检测SVN是否存在')
    if os.path.exists('D:\\svn\\svn.exe'):
        print("SVN检测存在！跳过下载！执行循环监听中 \n监听默认端口为：55555")
    else:
        retunPath = downloadURL('http://127.0.0.1/svn.zip', 'D:\\svn')
        SVNPATH = unzip(retunPath)
        print("下载解压SVN成功")
    try:
        tcpClientSock, addr = tcpServSock.accept()
        print("检测到客户已经连接，监听Commands执行：")
        print('客户IP地址是: ', addr)
        tcpClientSock.send(bytes('Hello Come From %s\'s Boy\nThis Auto Update Program!\nEnter [help] To Get Some Commands '%(str(addr[0])), 'utf-8'))

    except:
        print(Exception)
    while True:
        try:
            data = str(tcpClientSock.recv(BUFSIZ), encoding='utf-8')
            if not data:
                tcpClientSock.send(bytes('没有发送数据-----', 'utf-8'))

            if data == 'update gw':
                os.chdir('D:\\NewSiteForPython\\XPJ_New_Offical\\Test1')
                os.popen(SVNPATH+' update >gw.log.')
                fb=open('gw.log','r')
                res1=fb.read()
                print('客户输入的命令是:'+data)
                print('客户更新的内容是：'+res1)
                tcpClientSock.send(bytes('本次更新:%s' % (res1), 'utf-8'))
            elif data == 'help':
                print('Client Enter :'+data)
                tcpClientSock.send(bytes('''\n
               You Can Enter [update gw] to update 官网\n
               You Can Enter [update ag] to update 代理后台\n
               You Can Enter [update ag] to update 代理后台\n
               You Can Enter [update sr] to update Services\n
               You Can Enter [update ld] to update 充值中转 \n
                ''', 'utf-8'))
                #tcpClientSock.send(bytes('You Can Enter [update ag] to update 代理后台 \n', 'utf-8'))
                #tcpClientSock.send(bytes('You Can Enter [update ag] to update 代理后台 \n', 'utf-8'))
                #tcpClientSock.send(bytes('You Can Enter [update sr] to update Services \n', 'utf-8'))
                #tcpClientSock.send(bytes('You Can Enter [update ld] to update 充值中转 \n', 'utf-8'))
            elif data == 'update ag':
                print('Client Enter :' + data)
                tcpClientSock.send(bytes('Server-->%s' % (data), 'utf-8'))
            elif data == 'update be':
                print('Client Enter :' + data)
                tcpClientSock.send(bytes('Server-->%s' % (data), 'utf-8'))
            elif data == 'update sr':
                print('Client Enter :' + data)
                tcpClientSock.send(bytes('Server-->%s' % (data), 'utf-8'))
            elif data == 'update ld':
                print('Client Enter :' + data)
                tcpClientSock.send(bytes('Server-->%s' % (data), 'utf-8'))
            elif data== 'hello':
                print('Client Enter :' + data)
                tcpClientSock.send(bytes('You IP is %s !\nGood Lucky'%(str(addr[0])), 'utf-8'))
            else :
                print('Client Send Command is '+ data)
                tcpClientSock.send(bytes('Client Enter [help] To Get Some Help\nYou Send Command is %s' % (data), 'utf-8'))
        except Exception as e:
            print(e)
            print("远程断开连接、继续监听------")
            break

def GetIPList():
    import socket
    hostname = socket.gethostname()
    ip = socket.gethostbyname(hostname)
    return ip
def RunCommand():
    import os
    os.system('netstat -na')
def downloadURL(URL,path):
    ###下载 svn代码执行工具及dll
    import urllib.request
    print("当前目录====="+path+"：>>>>>>正在下载SVN...........")
    if not os.path.exists('d:\\svn'):
       os.mkdir('d:\\svn')
    f = urllib.request.urlopen(URL)
    date =f.read()
    with open(path+'\svn.zip','wb') as code :
        code.write(date)
        print("当前目录====="+path+"：>>>>>>正在解压SVN...........")

        return  path+'\svn.zip'
def unzip(path):
    #解压zip
    import zipfile
    f = zipfile.ZipFile(path, 'r')
    for file in f.namelist():
        f.extract(file, os.path.dirname(path))
    return os.path.dirname(path)
if __name__ == '__main__':
    import os
    from socket import *
    from time import ctime
    ip=GetIPList()
    print('当前服务器IP为:'+ip)
    while True:
        Server(ip)
